Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exists() to check for properties #17798

Merged

Conversation

noordawod
Copy link
Contributor

@noordawod noordawod commented Feb 6, 2024

Use exists() function to test for property existence on dictionaries/Maps rather than sprinkling the code with json.key === null || json.key === undefined.

In addition, the code removes the implicit null type of optional and required properties because in the case of TS/JS, restricting the property to only null means that it won't be able to receive the undefined value too, which in the case of JSON, ultimately resolves to null in transport.

export interface SomeInterface {
  requiredNullableProperty: string | null,
  requiredProperty: string,
  nullableProperty?: string,
}

The last line in the example covers the situation when you may pass in a value that is either null, undefined, or indeed a string. If you restrict it only to string | null, then undefined won't be a plausible value, which it should.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

@noordawod noordawod changed the title Use exists() to check for existing properties. Use exists() to check for properties Feb 6, 2024
@noordawod
Copy link
Contributor Author

noordawod commented Feb 11, 2024

Calling all codobots 🤖

@noordawod noordawod requested a review from wing328 February 12, 2024 19:10
@wing328 wing328 added this to the 7.4.0 milestone Feb 15, 2024
@noordawod noordawod requested review from wing328 and akehir February 15, 2024 10:11
@wing328 wing328 merged commit e025ef9 into OpenAPITools:master Feb 19, 2024
14 checks passed
@noordawod noordawod deleted the feature/typescript-fetch-exists branch February 19, 2024 15:12
kota65535 pushed a commit to kota65535/openapi-generator that referenced this pull request Feb 23, 2024
* Use `exists()` to check for existing properties.

* Generate Petstore code.

* Fix use of `null` instead of `undefined`.

* Enhance function code.

* Regenerate Petstore code.

* Use `exists()` in generated API clients.

* Use `exists()` in generated API clients.

* Refer to properties differently.
@tryforceful
Copy link

It seems to me that maybe something in this PR has introduced an unexpected behavior?

I have an OpenAPI schema definition as such:

{
  "components": {
    "schemas": {
	  "MyObject": {
		  "properties": {
		    "backtoken": {
		      "title": "Backtoken",
		      "maxLength": 500,
		      "type": "string",
		      "nullable": true
		    },
		    "skiptoken": {
		      "title": "Skiptoken",
		      "maxLength": 500,
		      "type": "string",
		      "nullable": true
		    }
		  },
		  "type": "object"
	  }
	}
  }
}

This is being generated with the typescript-fetch generator as:

export interface MyObject { 
    backtoken?: string; 
    skiptoken?: string;
}

Even though the openapi spec has an explicit mention of nullable: true.

Shouldn't it still honor that nullable: true property?

@noordawod
Copy link
Contributor Author

It seems to me that maybe something in this PR has introduced an unexpected behavior?

I have an OpenAPI schema definition as such:

{
  "components": {
    "schemas": {
	  "MyObject": {
		  "properties": {
		    "backtoken": {
		      "title": "Backtoken",
		      "maxLength": 500,
		      "type": "string",
		      "nullable": true
		    },
		    "skiptoken": {
		      "title": "Skiptoken",
		      "maxLength": 500,
		      "type": "string",
		      "nullable": true
		    }
		  },
		  "type": "object"
	  }
	}
  }
}

This is being generated with the typescript-fetch generator as:

export interface MyObject { 
    backtoken?: string; 
    skiptoken?: string;
}

Even though the openapi spec has an explicit mention of nullable: true.

Shouldn't it still honor that nullable: true property?

This was mentioned and clarified before in this comment: #17798 (comment)

@nielsvanvelzen
Copy link

This change breaks the OpenAPI spec. There is a difference between a nullable and optional property. With the release of 7.4.0 all properties that can be null (a value) are just made optional (omitted from request/response).

@hendrikpeilke
Copy link

hendrikpeilke commented Apr 25, 2024

@noordawod still getting back on this, reading carefully through the comments and reviews.

Your sentencte

The last line in the example covers the situation when you may pass in a value that is either null, undefined, or indeed a string. If you restrict it only to string | null, then undefined won't be a plausible value, which it should.

regarding this line

nullableProperty?: string,

I guess you're seeing the "pass in" from an API perspective, right? null, undefined or a string coming in from outside and is beeing parsed.

Seeing it the other way around, wanting to be able to make the resulting json in a request differentiate between null, undefined and a string is not possible with this solution, since typescript won't let you write nullableProperty = null;. Is that intended? I think it should be possible to assign null to a property defined as nullable in the openapi spec.

Other generators like sping, support differentiating between undefined, null and a string value. How would I achieve this by using the typescript-fetch-operator after this fix?

@l0gicgate
Copy link
Contributor

l0gicgate commented May 3, 2024

It appears that this PR has broken nullable types. null and undefined are two different things in TS.

Take the instance where we do a PATCH request and I want to partially update an entity. That means I would be able to pass in a partial object with subset of the fields.

Here's a definition of what a patch could look like, you can optionally pass any of the parameters. In this case, the subTitle field is also nullable, which would make it null in the database if I wanted to erase it:

export interface ArticlePatch {
    title?: string;
    subTitle?: string | null;
}

Scenario 1: Patching title only, I don't pass in subTitle

PATCH /articles/1
{
  "title": "My title",
}

Scenario 2: Patching subTitle only, making it null. Now I can't do this, because the types are broken:

PATCH /articles/1
{
  "subTitle": null,
}

subTitle is nullable: true in swagger.json:

{
  "subTitle": {
    "type": "string",
    "nullable": true
  }
}

In previous versions 7.3.0 and down the output was this:

export interface ArticlePatch {
    title?: string;
    subTitle?: string | null;
}

Since this PR has been merged:

export interface ArticlePatch {
    title?: string;
    subTitle?: string; // No longer nullable
}

EDIT:

I have raised a PR to address this and it has been merged:
#18887

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants